// VBDomErrorReporter.cpp

#include <iostream>
using namespace std;

#include "VBXML/VBXML.h" 
XERCES_CPP_NAMESPACE_USE


br::com::sbVB::VBXML::VBDomErrorReporter::VBDomErrorReporter()
{
	m_reportErrorsOnConsole = true;
	m_generateExceptionOnError = false;
}

void br::com::sbVB::VBXML::VBDomErrorReporter::setReportErrorsOnConsole(bool b)
{
	m_reportErrorsOnConsole  = b;
}

bool br::com::sbVB::VBXML::VBDomErrorReporter::getReportErrorsOnConsole()
{
	return m_reportErrorsOnConsole;
}

void br::com::sbVB::VBXML::VBDomErrorReporter::setGenerateExceptionOnError(bool b)
{
	m_generateExceptionOnError = b;
}

bool br::com::sbVB::VBXML::VBDomErrorReporter::getGenerateExceptionOnError()
{
	return m_generateExceptionOnError;
}

br::com::sbVB::VBLib::VBString 
br::com::sbVB::VBXML::VBDomErrorReporter::composeMessage(const char *message,
									 const xercesc::SAXParseException& toCatch)
{

	br::com::sbVB::VBLib::VBString lineFeed("\n");
	br::com::sbVB::VBLib::VBString ret = message + lineFeed;

	ret = message;
	ret += u2t(toCatch.getMessage());
	ret += lineFeed;
	ret += "Column:";
	ret += (int)toCatch.getColumnNumber();
	ret += lineFeed;
	ret += "Line:";
	ret += (int)toCatch.getLineNumber();
	// cout << "DEBUG: ret=" << ret << endl;
	return ret;

	// return br::com::sbVB::VBLib::VBString(message);
}


void br::com::sbVB::VBXML::VBDomErrorReporter::warning 
(const xercesc::SAXParseException& toCatch) 
{
	br::com::sbVB::VBLib::VBString message = 
		composeMessage("DomErrorReporter warning",toCatch);

	if (m_reportErrorsOnConsole)
		cout << message << endl;
	if (m_generateExceptionOnError)
		throw VBException_XML(message);
};
void br::com::sbVB::VBXML::VBDomErrorReporter::error (const xercesc::SAXParseException& toCatch)
{
	br::com::sbVB::VBLib::VBString message = 
		composeMessage("DomErrorReporter error",toCatch);
	if (m_reportErrorsOnConsole)
		cout << message << endl;
	if (m_generateExceptionOnError)
		throw VBException_XML(message);
};

void br::com::sbVB::VBXML::VBDomErrorReporter::fatalError (const xercesc::SAXParseException& toCatch)
{
	br::com::sbVB::VBLib::VBString message = 
		composeMessage("DomErrorReporter fatalError",toCatch);
	if (m_reportErrorsOnConsole)
		cout << message << endl;
	if (m_generateExceptionOnError)
		throw VBException_XML(message);
};

void br::com::sbVB::VBXML::VBDomErrorReporter::handleError(const xercesc::SAXParseException& toCatch)
{
	br::com::sbVB::VBLib::VBString message = 
		composeMessage("DomErrorReporter handleError",toCatch);
	if (m_reportErrorsOnConsole)
		cout << message << endl;
	if (m_generateExceptionOnError)
		throw VBException_XML(message);
};

void br::com::sbVB::VBXML::VBDomErrorReporter::resetErrors()
{
	br::com::sbVB::VBLib::VBString message = "DomErrorReporter resetErrors";
	if (m_reportErrorsOnConsole)
		cout << message << endl;
	if (m_generateExceptionOnError)
		throw VBException_XML(message);
};
